# -*- coding: utf-8 -*-
# !/usr/bin/env python
import numpy as np


def box_error(series, rate=1.5):
    """
    通过箱型图法判断一组数据中的异常值
    :param series: Series或List等
    :param rate: 箱型图的倍率，统计角度取1.5
    :return: error数据
    """
    _q1_ = np.quantile(series, 0.25)
    _q3_ = np.quantile(series, 0.75)
    _min_ = _q1_ - rate * (_q3_ - _q1_)
    _max_ = _q3_ + rate * (_q3_ - _q1_)
    result = np.where((series > _min_) & (series < _max_), '正常', '异常')
    return result


def sigma_error(series, rate=3):
    """
    通过3sigma法判断一组数据中的异常值
    :param series: Series或List等
    :param rate: sigma倍率，一般取3
    :return: error数据
    """
    _min_ = np.mean(series) - rate * np.std(series)
    _max_ = np.mean(series) + rate * np.std(series)
    result = np.where((series > _min_) & (series < _max_), '正常', '异常')
    return result


def rate_error(series, rate=0.1):
    """
    通过阈值法判断一组数据中的异常值
    :param series: Series或List等
    :param rate: 阈值
    :return: error数据
    """
    _min_ = np.mean(series) * (1 - rate)
    _max_ = np.mean(series) * (1 + rate)
    result = np.where((series > _min_) & (series < _max_), '正常', '异常')
    return result
